package com.hdey.service.max.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdey.mapper.max.TbUscSupclaMapper;
import com.hdey.pojo.max.TbUscSupcla;
import com.hdey.service.max.TbUscSupclaSvs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("tbUscSupclaSvs")
@Transactional
@Slf4j
public class TbUscSupclaSvsImpl extends ServiceImpl<TbUscSupclaMapper, TbUscSupcla> implements TbUscSupclaSvs{


    @Resource
    private TbUscSupclaMapper tbUscSupclaMapper;

    @Override
    public List<TbUscSupcla> getTbUscSupcla(Integer supclaId, String supclaFlag){
        // System.out.println(supclaId+"----"+supclaFlag);
        LambdaQueryWrapper<TbUscSupcla> wrapper = new LambdaQueryWrapper<>();

        if(supclaId != null && !"".equals(supclaId)){
            wrapper.eq(TbUscSupcla::getSupclaId,supclaId);
        }

        if(supclaFlag != null && !"".equals(supclaFlag) && !"All".equals(supclaFlag)){
            wrapper.eq(TbUscSupcla::getSupclaFlag,supclaFlag);
        }

        List<TbUscSupcla> list = tbUscSupclaMapper.selectList(wrapper);

        for(TbUscSupcla supcla : list){
            if(supcla.getSupclaFlag().equals("N")){
                supcla.setSupclaFlag("无供应商");
            }else if(supcla.getSupclaFlag().equals("Y")){
                supcla.setSupclaFlag("有供应商");
            }
        }
        return list;
    }

    @Override
    public int updateSupcla(TbUscSupcla supcla) {
        LambdaUpdateWrapper<TbUscSupcla> Wrapper = new LambdaUpdateWrapper<>();
        Wrapper.eq(TbUscSupcla::getSupclaId, supcla.getSupclaId())
                .set(TbUscSupcla::getSupclaName,supcla.getSupclaName())
                .set(TbUscSupcla::getSupclaDescription,supcla.getSupclaDescription())
                .set(TbUscSupcla::getUpdateBy,"admin");

        int result = tbUscSupclaMapper.update(null,Wrapper);

        return result;
    }


    @Override
    public int deleteSupcla(Integer supclaId) {

        int result = tbUscSupclaMapper.deleteById(supclaId);

        return result;
    }

    @Override
    public int createSupcla(TbUscSupcla supcla) {
        supcla.setCreateBy("admin");
        int result = tbUscSupclaMapper.insert(supcla);
        return result;
    }


}
